package mnstudent.dataacesslayer;

import mnstudent.datatranferobject.MajorOfSubjectDetailDTO;
import mnstudent.datatranferobject.PrerequisiteSubjectDetailDTO;
import mnstudent.datatranferobject.SubjectDTO;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author trungpv
 */
public class SubjectDAL {

    //<editor-fold defaultstate="collapsed" desc="Attributes">
    private SubjectDTO m_SubjectObj;
    private DataExecuteUseStoreProcedure m_DataExecute;
    private String m_sqlCmd;

    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="Constuctor">
    public SubjectDAL() {
        this.m_SubjectObj = new SubjectDTO();
        this.m_DataExecute = new DataExecuteUseStoreProcedure();
        this.m_sqlCmd = "";
    }
   //</editor-fold>  

    //<editor-fold defaultstate="collapsed" desc="Method">
    public ResultSet getAllSubject() {
        try {
            m_sqlCmd = "{Call GetAllSubjectFromDatabase}";
            ResultSet _rsData = this.m_DataExecute.getDataFromDatabase(m_sqlCmd, null);
            return _rsData;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public ResultSet getAllPrerequisiteSubjectOfSubjectByIdFromDatabase(String _id) {
        try {
            m_sqlCmd = "{Call GetInfoPrerequisiteSubjectByIdSubjectFromDatabase(?)}";
            String[] arrId = {_id};
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public ResultSet getAllMajorOfSubjectByIdFromDatabase(String _id) {
        try {
            m_sqlCmd = "{Call GetInfoMajorByIdSubjectFromDatabase(?)}";
            String[] arrId = {_id};
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public ResultSet getAllMajorFromDatabase() {
        try {
            m_sqlCmd = "{Call GetAllMajorFromDatabase}";
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, null);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public ResultSet getAllOrderSemesterFromDatabase() {
        try {
            m_sqlCmd = "{Call GetAllOrderSemesterFromDatabase}";
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, null);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    /**
     * Ham xoa mot MONHOC ra khoi database
     *
     * @param _subject
     * @return
     */
    public boolean deleteSubjectFromDatabase(SubjectDTO _subject) {
        try {
            m_sqlCmd = "{Call DeleteSubjectFromDatabase(?)}";
            String[] arrStr = {_subject.getSubjectID()};

            return this.m_DataExecute.updateDataToDatabase(m_sqlCmd, arrStr) > 0;

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    /**
     * Ham insert mot sinh vien xuong database
     *
     * @param _studentObj
     * @return
     */
    public boolean insertSubjectToDatabase(SubjectDTO _subjectObj) {
        try {
            m_sqlCmd = "{Call InsertSubjectToDatabase(?,?,?,?)}";
            Object[] arrStr = {_subjectObj.getSubjectID(), _subjectObj.getNameOfSubject(), _subjectObj.getNumberOfCredit(),
                _subjectObj.getKindOfSubjectID()};

            return this.m_DataExecute.updateDataToDatabase(m_sqlCmd, arrStr) > 0;

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public boolean insertPrerequisiteSubjectDetail(PrerequisiteSubjectDetailDTO subPrerequisiteSubjectObj) {
        try {
            m_sqlCmd = "{Call InsertPrerequisiteSubjectDetailToDatabase(?,?)}";
            String[] arrStr = {subPrerequisiteSubjectObj.getSubjectID(), subPrerequisiteSubjectObj.getPrerequisiteSubjectID()};

            return this.m_DataExecute.updateDataToDatabase(m_sqlCmd, arrStr) > 0;

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public boolean insertArrayPrerequisiteSubjectDetail(ArrayList<PrerequisiteSubjectDetailDTO> arr) {
        boolean rs = false;
        try {
            for (PrerequisiteSubjectDetailDTO i : arr) {
                if (insertPrerequisiteSubjectDetail(i)) {
                    rs = true;
                } else {
                    rs = false;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return rs;
    }

    public boolean updateSubjectToDatabase(SubjectDTO subjectObj) {
        try {
            m_sqlCmd = "{Call UpdateSubjectToDatabase(?,?,?,?)}";
            Object[] arrStr = {subjectObj.getSubjectID(), subjectObj.getNameOfSubject(), subjectObj.getNumberOfCredit(),
                subjectObj.getKindOfSubjectID()};

            return this.m_DataExecute.updateDataToDatabase(m_sqlCmd, arrStr) > 0;

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public boolean deletePrerequisiteSubjectDetailFromDatabase(String subjectID) {
        try {
            m_sqlCmd = "{Call DeletePrerequisiteSubjectDetailFromDatabase(?)}";
            Object[] arrStr = {subjectID};

            return this.m_DataExecute.updateDataToDatabase(m_sqlCmd, arrStr) >= 0;

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public ResultSet getAllPrerequisiteSubject() {
        try {
            m_sqlCmd = "{Call GetAllPrerequisiteSubjectFromDatabase}";
            ResultSet _rsData = this.m_DataExecute.getDataFromDatabase(m_sqlCmd, null);
            return _rsData;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
    
    public ResultSet getIDSubjectFromDatabase() {
        try {
            m_sqlCmd = "{Call GetIDSubjectFromDatabase}";
            ResultSet _rsData = this.m_DataExecute.getDataFromDatabase(m_sqlCmd, null);
            return _rsData;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
    
    //</editor-fold>

    public boolean deleteMajorOfSubjectDetailFromDatabase(String subjectID) {
        try {
            m_sqlCmd = "{Call DeleteMajorOfSubjectDetailFromDatabase(?)}";
            Object[] arrStr = {subjectID};

            return this.m_DataExecute.updateDataToDatabase(m_sqlCmd, arrStr) >= 0;

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public boolean insertMajorOfSubjectDetail(MajorOfSubjectDetailDTO subMajorOfSubjectObj) {
         try {
            m_sqlCmd = "{Call InsertMajorOfSubjectDetailToDatabase(?,?,?)}";
            String[] arrStr = {subMajorOfSubjectObj.getMajorID(), subMajorOfSubjectObj.getOrderSemesterID(), subMajorOfSubjectObj.getSubjectID()};

            return this.m_DataExecute.updateDataToDatabase(m_sqlCmd, arrStr) > 0;

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public boolean insertArrayMajorOfSubjectDetail(ArrayList<MajorOfSubjectDetailDTO> arr) {
         boolean rs = false;
        try {
            for (MajorOfSubjectDetailDTO i : arr) {
                if (insertMajorOfSubjectDetail(i)) {
                    rs = true;
                } else {
                    rs = false;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return rs;
    }
}
